local TARGET_MAP = "MAP40"
local GIVE_TIME = 210 * TICRATE
local triggered = false

addHook("MapLoad", function()
    triggered = false
end)

addHook("ThinkFrame", function()
    if G_BuildMapName(gamemap) ~= TARGET_MAP then return end
    if leveltime < GIVE_TIME then return end
    if not (gametype == GT_MATCH or gametype == GT_TAG or gametype == GT_HIDEANDSEEK) then return end

    if not triggered then
        triggered = true
        print(string.char(130).."[WARNING]: "..string.char(128).."initializing..."..string.char(129).."TRASHDOWN !")
    end

    for player in players.iterate do
        if not (player and player.valid and player.mo) then continue end

        -- Condition IT pour Tag/H&S (Le Match donne à tout le monde)
        if (gametype == GT_TAG or gametype == GT_HIDEANDSEEK) and not (player.pflags & PF_TAGIT) then
            continue
        end

        -- 1. Partie invincibilité pour les IT et joueurs MATCH
        -- On donne un bouclier normal (SH_FORCE ou SH_PITY)
        -- Mais on ne joue AUCUN son pour ne pas spammer les oreilles
        if not (player.powers[pw_shield] & SH_PITY) then
            player.powers[pw_shield] = SH_PITY
        end
        
        -- On rend le bouclier invisible sur le personnage
        if player.mo.shieldobj and player.mo.shieldobj.valid then
            player.mo.shieldobj.renderfx = RF_INVISIBLE
        end

        -- 2. La ligne magique pour débloquer tt les pannels :3333 (le pur chaos)
        player.ringweapons = $ | RW_AUTO | RW_BOUNCE | RW_SCATTER | RW_GRENADE | RW_EXPLODE | RW_RAIL

        -- 3. les muni à l'infini (Anti-lag)
        if player.powers[pw_automaticring] < 999 then player.powers[pw_automaticring] = 999 end
        if player.powers[pw_bouncering] < 999 then player.powers[pw_bouncering] = 999 end
        if player.powers[pw_scatterring] < 999 then player.powers[pw_scatterring] = 999 end
        if player.powers[pw_grenadering] < 999 then player.powers[pw_grenadering] = 999 end
        if player.powers[pw_explosionring] < 999 then player.powers[pw_explosionring] = 999 end
        if player.powers[pw_railring] < 999 then player.powers[pw_railring] = 999 end

        -- 4. le ring de l'infini
        if player.rings < 1 then
            player.rings = 1
        end
    end
end)